<?php
namespace Admin\Controller;
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
use Admin\Controller\AdminController;

class CouponController extends AdminController{

    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'operate'); // 数据集
    }
    
        //满减送
    public function index(){
        $pMan = D('PMansong');
        if(IS_GET){
            $keywords = I('get.keywrods');
            $str_time = I('get.query_start_time');
            $end_time = I('get.query_end_time');
            
            if(!empty($keywords)){
                $datas['store_name'] = array('like',"%$keywords%");
            }
            if(!empty($str_time)){
                $query_start_time = strstr($str_time,' ',true);
                $datas['start_time'] = array('egt',$query_start_time);
            }
            if(!empty($end_time)){
                $query_end_time = strstr($end_time,' ',true);
                $datas['end_time'] = array('elt',$query_end_time);
            }
        }
        $count = $pMan->relation('pMansong')->where($datas)->order('mansong_id desc')->count();//获得满足条件的记录数
        $Page  = new \Think\Page($count,10);//实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        $list = $pMan->relation('pMansong')->where($datas)->order('mansong_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 数据集
        $this->assign('page',$show);// 页码
        $this->assign('keywords',$keywords);// 关键词
        $this->assign('action', 'coupon');
        $this->display();
    }
    
    
    //增加满减送
    public function add(){
        if(IS_POST){
            $title = I('title');
            $price = I('mansong_price')[0];
            $discount = I('mansong_discount')[0];
            if(empty($title)){
                $this->error('标题不能为空！');
            }
            if(empty($price)|| empty($discount)){
                $this->error('优惠规则必须有一条！');
            }else{
                $datas['mansong_name']=I('title');
                $datas['quota_id']=  1;
                $datas['start_time']=strtotime(I('start_time'));
                $datas['end_time']=strtotime(I('end_time'));
                $datas['member_id']= session('manager_id');
                $datas['store_id']= 0;
                $datas['member_name']=  session('manager_auth')['truename'];
                $datas['store_name']='淘嘟嘟商城';
                $datas['state']=2;
                $datas['remark']=I('remark');
                $re = M('pMansong')->add($datas);
                if($re){
                    foreach (I('mansong_price') as $key => $value) {
                        $data['mansong_id']=$re;
                        $data['price']=$value;
                        $data['discount']=I('mansong_discount')[$key];
                        $data['mansong_goods_name']="";
                        $data['goods_id']="";
                        M('pMansongRule')->add($data);
                    }
                    $this->success("优惠券添加成功！",U('index'));
                }
            }
        }
        $this->assign('action', 'coupon');
        $this->display();
    }
    
    
    //修改满减送
    public function edit(){
        if(IS_POST){
            $ids = I('mansong_id');
            $maps['mansong_id'] = $ids;
            $datas['mansong_name']=I('title');
            $datas['quota_id']=  1;
            $datas['start_time']=strtotime(I('start_time'));
            $datas['end_time']=strtotime(I('end_time'));
            $datas['member_id']= session('manager_id');
            $datas['store_id']= 0;
            $datas['member_name']=  session('manager_auth')['truename'];
            $datas['store_name']='淘嘟嘟平台';
            $datas['state']=2;
            $datas['remark']=I('remark');
            $re = M('pMansong')->where($maps)->save($datas);
            if($re !== false){
                //先将以前的优惠信息删除
                M('pMansongRule')->where($maps)->delete();
                foreach (I('mansong_price') as $key => $value) {
                    $data['mansong_id']=$ids;
                    $data['price']=$value;
                    $data['discount']=I('mansong_discount')[$key];
                    $data['mansong_goods_name']="";
                    $data['goods_id']="";
                    M('pMansongRule')->add($data);
                }
                $this->success("优惠券修改成功！",U('index'));
            }
        }
        
        $id = I('mansong_id');
        $map['mansong_id'] = $id;
        $pMan = D('PMansong');
        $re = $pMan->relation('pMansong')->where($map)->find();
        $this->assign('re',$re);
        $this->assign('action', 'coupon');
        $this->display();
    }
    
    
    //删除满减送
    public function del(){
        
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('pMansong')->where('mansong_id=' . $Id)->delete()) {
            M('pMansongRule')->where('mansong_id=' . $Id)->delete();
            $this->success('删除成功！', U('index'));
        } else {
            $this->error('删除失败！');
        }
    }
    
    
}
